package com.ranshiyan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.ranshiyan.entity.Link;
import com.ranshiyan.entity.User;
import com.ranshiyan.service.ArticleService;
import com.ranshiyan.service.LinkService;
import com.ranshiyan.service.UserService;

@RequestMapping("admin")
@Controller
public class AdminController {
	
	@Autowired
	UserService userService;
	
	@Autowired
	ArticleService articleService;
	
	@Autowired
	LinkService linkService;
	
	@RequestMapping("index")
	public String index() {
		return "admin/index";
	}
	
	@RequestMapping("userlist")
	public String userlist(HttpServletRequest request,@RequestParam(defaultValue="1")int page) {
		PageInfo<User> list = userService.list(page);
		request.setAttribute("userPage", list);
		return "admin/userlist";
	}
	
	@RequestMapping("articlelist")
	public String articlelist(HttpServletRequest request,@RequestParam(defaultValue="1")int page) {
		PageInfo articlePage = articleService.list(page);
		List list = articlePage.getList();
		request.setAttribute("articlePage", articlePage);
		return "admin/articlelist";
	}
	
	//友情链接管理
	@RequestMapping("linklist")
	public 	String linklist(HttpServletRequest request,@RequestParam(defaultValue="1") int page) {
		PageInfo<Link> pageLink = linkService.list(page);
		request.setAttribute("pageLink", pageLink);
		return "admin/linkList";	
	}
	
	@RequestMapping(value="addlink")
	public String addlink(HttpServletRequest request) {
		return "admin/link/add";
	}
	
	@RequestMapping("addLinkDate")
	@ResponseBody
	public String addLinkDate(HttpServletRequest request,Link link) {
		int result = linkService.add(link);
		if(result<1) {
			return "failed";
		}
		return "success";
	}
	
	@RequestMapping("delLink")
	@ResponseBody
	public String delLink(HttpServletRequest request,int id) {
		int result = linkService.delLink(id);
		if(result<1) {
			return "failed";
		}
		return "success";
	}
	
	//修改页面
	@RequestMapping("toUodatelink")
	public String toUodatelink(HttpServletRequest request,int id) {
		Link link = linkService.get(id);
		request.setAttribute("link", link);
		return "admin/link/update";
	}
	@RequestMapping("updateLinkDate")
	@ResponseBody
	public String updateLinkData(HttpServletRequest request,Link link) {
		int update = linkService.update(link);
		return update>0?"success":"failed";
	}
}
